package com.lilihub.miaosha.dao;

import com.lilihub.miaosha.domain.MiaoshaGoods;
import com.lilihub.miaosha.vo.GoodsVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @BelongsProject: miaosha
 * @BelongsPackage: com.lilihub.miaosha.dao
 * @Author: LiYing
 * @CreateTime: 2021-04-26 20:30
 * @Description:
 */

@Mapper
public interface GoodsDao {

    @Select("select g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price" +
            " from miaosha_goods mg left join goods g on mg.goods_id = g.id")
    List<GoodsVo> getGoodsVoList();


    @Select("select g.*,mg.stock_count,mg.start_date,mg.end_date,mg.miaosha_price" +
            " from miaosha_goods mg left join goods g on mg.goods_id = g.id where g.id = #{goodsId}")
    GoodsVo getGoodsByGoodsId(@Param("goodsId") Long goodsId);

    //数据库在更新数据时，会对该条数据上锁，行锁（innodb），不会出现多条线程修改一个数据的情况
    @Update("update miaosha_goods set stock_count = stock_count - 1 where goods_id = #{goods.goodsId} and stock_count > 0")
    int reduceStock(@Param("goods") MiaoshaGoods goods);

    @Update("update miaosha_goods set stock_count = 10 where goods_id = #{goods.id}")
    void resetStock(@Param("goods")GoodsVo goodsVo);
}
